package com.xsmart.oa.dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 用户收藏
 *
 * @author guojd
 */
@Component
public interface UserFavoritesDao {
    /**
     * 用户收藏商品
     * @param uid 用户id
     * @param goodsId 商品id
     * @return 影响记录数
     */
    @Insert("INSERT INTO infu_user_favorites_goods(user_id, goods_id) VALUES(#{uid},#{goodsId})")
    int addGoodsUserFavorites(@Param("uid") Integer uid, @Param("goodsId") Integer goodsId);

    /**
     * 查询用户收藏商品
     *
     * @param ufgId 收藏id
     * @param uid 用户id
     * @param goodsId 商品id
     * @return 收藏列表
     */
    @Select("<script>" +
            "select t.ufg_id id,t.user_id,t.goods_id,DATE_FORMAT(t.add_time,'%Y-%m-%d %T') add_time,  " +
            "tt.goods_name,tt.goods_img, tt.is_promote,tt.promote_price,tt.shop_price " +
            "from infu_user_favorites_goods t " +
            "left join goods tt on t.goods_id=tt.goods_id " +
            " <where>" +
            " <if test=\"ufgId!=null\"> t.ufg_id=#{ufgId}</if>" +
            " <if test=\"uid!=null\"> and t.user_id=#{uid}</if>" +
            " <if test=\"goodsId!=null\"> and t.goods_id=#{goodsId}</if>" +
            "</where>" +
            "</script>")
    List<Map<String,Object>> queryGoodsUserFavorites(@Param("ufgId") Integer ufgId, @Param("uid") Integer uid, @Param("goodsId") Integer goodsId);

    /**
     * 用户取消收藏
     * @param ufgId
     * @return
     */
    @Delete("delete from infu_user_favorites_goods where ufg_id=#{ufgId}")
    int removeGoodsUserFavorites(Integer ufgId);

    /**
     * 从商品详情页面中取消收藏
     * @param userId
     * @param goodsId
     * @return
     */
    @Delete("DELETE FROM infu_user_favorites_goods where user_id=#{userId} and goods_id=#{goodsId}")
    int deleteGoodsUserFavorites(@Param("userId") Integer userId,@Param("goodsId") Integer goodsId);
}
